package com.gec.shop;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("localhost",9996);
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        TransferFiles transferFiles = new TransferFiles();
        Scanner scanner = new Scanner(System.in);
        Scanner sc = new Scanner(socket.getInputStream());
        String str;
        boolean isLogin = false;
        int userId = 0;

        //登录与注册
        while(true){
            while(!isLogin){
                transferFiles.getDatas().clear();
                System.out.println("\uD83C\uDF6E\uD83C\uDF6D\uD83C\uDF6C 会员 \uD83C\uDF69\uD83C\uDF6A 员工 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E");
                str = scanner.next();
                if(str.contains("会员")){
                    System.out.println("\uD83C\uDF6E\uD83C\uDF6D\uD83C\uDF6C 注册 \uD83C\uDF69\uD83C\uDF6A 登录 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E");
                    str = scanner.next();
                    if(str.contains("注册")){
                        Tool.allInOneOperation(out, transferFiles, sc, "member_register",Member.memberRegister(scanner));
                    }else if(str.contains("登录")){
                        isLogin = Tool.allInOneOperation(out,transferFiles,sc,"member_login",Member.memberLogin(scanner));
                        if(isLogin){
                            userId = ((Member)(transferFiles.getDatas().get("member_login"))).getId();
                        }
                    }else{
                        System.out.println("\uD83D\uDD27 error:未知操作 \uD83D\uDD28");
                    }
                }else if(str.contains("员工")){
                    System.out.println("\uD83C\uDF6E\uD83C\uDF6D\uD83C\uDF6C 注册 \uD83C\uDF69\uD83C\uDF6A 登录 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E");
                    str = scanner.next();
                    if(str.contains("注册")){
                        Tool.allInOneOperation(out, transferFiles, sc, "staff_register",Staff.staffRegister(scanner));
                    }else if(str.contains("登录")){
                        isLogin = Tool.allInOneOperation(out,transferFiles,sc,"staff_login",Staff.staffLogin(scanner));
                        if(isLogin){
                            userId = ((Staff)(transferFiles.getDatas().get("staff_login"))).getId();
                        }
                    }else{
                        System.out.println("\uD83D\uDD27 error:未知操作 \uD83D\uDD28");
                    }
                }else if(str.contains("退出")){
                    System.out.println("客户端已经退出");
                    sc.close();
                    scanner.close();
                    out.close();
                    socket.close();
                    return;
                }
            }
            while (isLogin){
                //登录之后的操作
                if(transferFiles.getTarget().contains("member")){
                    System.out.println("\uD83C\uDF6E\uD83C\uDF6D\uD83C\uDF6C 会员 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E");
                    System.out.println("\uD83C\uDF6E\uD83C\uDF6D\uD83C\uDF6C 查询商品 \uD83C\uDF69\uD83C\uDF6A 余额充值  \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E 我的购物车 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E 添加订单 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E 退出登录");
                    str = scanner.next();
                    if(str.contains("查询商品")){
                        Tool.allInOneOperation(out,transferFiles,sc,"member_query_commodity",null);
                    }else if(str.contains("购物车")){
                        while(true){
                            System.out.println("\uD83C\uDF6E\uD83C\uDF6D\uD83C\uDF6C 我的购物车 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E");
                            System.out.println("\uD83C\uDF6E\uD83C\uDF6D 添加购物车 \uD83C\uDF69\uD83C\uDF6A 修改购物车 \uD83C\uDF69\uD83C\uDF6A 查看购物车 \uD83C\uDF69\uD83C\uDF6A 删除购物车 \uD83C\uDF69\uD83C\uDF6A 退出购物车 \uD83C\uDF6A");
                            str = scanner.next();
                            if(str.contains("添加")){
                                Tool.allInOneOperation(out,transferFiles,sc,"member_shoppingCart_add",userId,Commodity.shoppingAddCommodity(scanner));
                            }else if(str.contains("查")){
                                Tool.allInOneOperation(out,transferFiles,sc,"member_shoppingCart_query",userId,null);
                            }else if(str.contains("改")){
                                Tool.allInOneOperation(out,transferFiles,sc,"member_shoppingCart_xg",userId,Commodity.shoppingXgCommodity(scanner));
                            } else if(str.contains("删")){
                                Tool.allInOneOperation(out,transferFiles,sc,"member_shoppingCart_delect",userId,Commodity.shoppingdelectCommodity(scanner));
                            } else if(str.contains("退出")){
                                break;
                            }else{
                                System.out.println("error:未知操作 请重试");
                            }
                        }
                    }else if(str.contains("添加")){
                        System.out.println("正在结算购物车并生成订单");
                        Tool.allInOneOperation(out,transferFiles,sc,"member_order_make",userId,null);
                    }else if(str.contains("充值")){
                        Tool.allInOneOperation(out, transferFiles,sc,"member_addBalance",userId,Member.addbalance(scanner));
                    } else if(str.contains("退出")){
                        isLogin = false;
                        break;
                    }else{
                        System.out.println("未知异常！！");
                    }
                }else if(transferFiles.getTarget().contains("staff")){
                    System.out.println("\uD83C\uDF6E\uD83C\uDF6D\uD83C\uDF6C 员工 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E");
                    System.out.println("\uD83C\uDF6E\uD83C\uDF6D\uD83C\uDF6C 查询商品 \uD83C\uDF69\uD83C\uDF6A 商品入库  \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E 修改商品 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E 员工打卡 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E 查询考勤 \uD83C\uDF69\uD83C\uDF6A\uD83E\uDF6E 退出登录");
                    str = scanner.next();
                    if(str.contains("入库")){
                        Tool.allInOneOperation(out,transferFiles,sc,"staff_add_commodity",userId,Commodity.newCommodity(scanner));
                    }else if(str.contains("查询")){
                        Tool.allInOneOperation(out,transferFiles,sc,"staff_query_commodity",null);
                    } else if (str.contains("修改")) {
                        Tool.allInOneOperation(out,transferFiles,sc,"staff_modity_commodity",userId,Commodity.modityCommdity(scanner));
                    } else if (str.contains("打卡")) {
                        Tool.allInOneOperation(out,transferFiles,sc,"staff_clockIn",userId,null);
                    }else if (str.contains("考勤")) {
                        Tool.allInOneOperation(out,transferFiles,sc,"staff_attendanceRecord",userId,null);
                    }else if(str.contains("退出")){
                        System.out.println("客户端已经退出");
                        sc.close();
                        scanner.close();
                        out.close();
                        socket.close();
                        return;
                    }
                }
            }
        }
    }
static class GetServerMes{
    public static boolean getServerMes(Scanner scanner){
        String str;
        while(scanner.hasNext()){
            str = scanner.nextLine();
            System.out.println("来自服务端信息--->"+str);
            if(str.contains("成功")){
                return true;
            }else if(str.contains("错误") || str.contains("失败")){
                return false;
            }
        }
        return false;
    }
}
static class Tool{
    public static <A> Boolean allInOneOperation(ObjectOutputStream out, TransferFiles transferFiles, Scanner sc, String str,int id,A a) throws IOException {
        transferFiles.setTarget(str);
        transferFiles.getDatas().put(str,a);
        transferFiles.setId(id);
        out.reset();
        out.writeObject(transferFiles);//传递封装数据给server
        return (Boolean) GetServerMes.getServerMes(sc);//接受服务端返回信息
    }
    public static <A> Boolean allInOneOperation(ObjectOutputStream out, TransferFiles transferFiles, Scanner sc, String str,A a) throws IOException {
        transferFiles.setTarget(str);
        transferFiles.getDatas().put(str,a);
        out.reset();
        out.writeObject(transferFiles);//传递封装数据给server
        return (Boolean) GetServerMes.getServerMes(sc);//接受服务端返回信息

    }
}
}
